package com.ding.listener;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/*
 * 一个Bean的方法使用@RabbitListener注解,这个方法就会监听指定的队列
 */

@Component
public class MsgListener {


    /*

        
        //检查消费队列的消费者数量
        RabbitAdmin admin = context.getBean(RabbitAdmin.class);
        System.out.println(admin.getQueueInfo("canal-market-user").getConsumerCount());
   
        /*
        //动态注册一个消费者
         */
        RabbitAdmin rabbitAdmin = context.getBean(RabbitAdmin.class);
        ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();

        container.setAmqpAdmin(rabbitAdmin);
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames("canal-market-user");
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
        container.setMessageListener(message -> {
            byte[] body = message.getBody();
            String s = new String(body);
            System.out.println(s);
        });
        container.start();


    */
	
	@RabbitListener(queues="a")
	public void hand_Direct(String msg) {
		System.out.println("a消息:"+msg);
	}
	

	@RabbitListener(queues="b")
	public void hand_Topic(String msg) {
		System.out.println("b消息:"+msg);
	}


	 @RabbitListener(queues = "test")
    public void test(Message message, Channel channel) throws IOException {

		//当抛出 AmqpRejectAndDontRequeueException 异常的时候，则消息会被拒绝，且 requeue = false（不重新入队列）

		//当抛出 ImmediateAcknowledgeAmqpException 异常，则消费者会被确认
			//拒收消息,并且消息不再重新放回队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);

    }

	
}
